package com.mybatis02.mapper;

import com.mybatis02.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author wzm
 * @create 2023-09-26 14:45
 * project_name:javaweb2308
 */
//emp数据访问接口

@Mapper
public interface EmpMapper {
    /**
     * 根据id删除员工信息
     * @param id  要删除的id
     */
    @Delete("delete from emp where id = #{uuu}")
     void delete(Integer id);

    //新增
    //#{}取的是参数对象的属性名
    //如果新增数据以后将新增后的主键返回给别的地方使用
     @Options(useGeneratedKeys = true,keyProperty = "id")
     @Insert("insert  into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) " +
             "values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entryDate}, #{deptId}, #{createTime}, #{updateTime})")
     int  insert(Emp  emp);

     //更新方法
     @Update("update emp " +
             "set username = #{username},name = #{name},gender = #{gender},image = #{image},entrydate=#{entryDate},update_time = #{updateTime} " +
             "where id = #{id}")
     void update(Emp emp);

     //根据id查询对象信息的方法
    //别名
    /*@Select("select id,username,name,password,gender,image,job,entrydate,dept_id deptId,create_time createTime,update_time updateTime" +
            " from emp where id = #{id}")
    Emp getById(Integer id);*/


    //手动使用结果映射注解实现解决  column 表示的数据库字段  property表示是实体属性
/*    @Results({
            @Result(column = "dept_id",property = "deptId"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select id,username,name,password,gender,image,job,entrydate,dept_id,create_time,update_time" +
            " from emp where id = #{id}")
    Emp getById(Integer id);*/


    //已经开启了驼峰命名 根据id查询
    @Select("select id,username,name,password,gender,image,job,entrydate,dept_id,create_time,update_time" +
            " from emp where id = #{id}")
    Emp getById(Integer id);

   /* //条件查询的方法  ''中不能直接使用功能#{} 需要用${}
    @Select("select * from emp " +
            "where name like '%${name}%' " +
            "and gender = #{gender} " +
            "and entryDate between  #{begin} and #{end} " +
            "order by update_time desc")
    List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);*/


    //条件查询的方法  ''中不能直接使用功能#{} 需要用${}
    @Select("select * from emp " +
            "where name like concat('%',#{name},'%') " +
            "and gender = #{sex} " +
            "and entryDate between  #{begin} and #{end} " +
            "order by update_time desc")
    List<Emp> list(@Param("name") String name,@Param("sex") Short gender, LocalDate begin, LocalDate end);

}
